<?php
/**
 * User: hjd-yao
 * Date: 2018/11/20
 * Time: 15:39
 */

namespace App\Http\Controllers\Backend;

use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\System\AuthModel;
use App\Models\System\RoleModel;
use Illuminate\Http\Request;

class RoleController extends BackendController
{

    /**
     * @title 网站所有角色
     */
    public function showRoles()
    {
        $list = RoleModel::where('status',1)->get();
        return view('backend::roles.list_roles',[
            'page_title'=>'管理角色',
            'list'=>$list,
            'action_title'=>AuthModel::init()->getAllActionsTitle()
        ]);
    }

    /**
     * @title 添加角色
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showAddRoleForm(Request $request)
    {
        if($request->isMethod('post')){
            $data = $request->post();
            if(RoleModel::init()->addRole($data)){
                return SelfResponse::success('添加成功','b_Role_showRoles');
            }
            return SelfResponse::error('添加失败');
        }else{
            return view('backend::roles.add_role',[
                'page_title'=>'添加角色',
                'list'=>json_encode(AuthModel::init()->getDTreeActions())
            ]);
        }
    }


    /**
     * @title 启用/禁用角色
     * @param int $role_id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function switchRole(int $role_id)
    {
        $role = RoleModel::find($role_id);
        if(!$role){
            return SelfResponse::error('信息不存在');
        }
        $status = abs($role->status-1);
        $role->status = $status;
        $role->updated_at = time();
        $role->updated_ip = get_client_ip(1);
        if($role->save()){
            return SelfResponse::success('操作成功');
        }
        return SelfResponse::error('操作失败');
    }


    /**
     * @title 编辑角色
     * @param Request $request
     * @param int $role_id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function editRole(Request $request,int $role_id)
    {
        $info = RoleModel::find($role_id);
        if($request->isMethod('post')){
            $data = $request->post();
            if(isset($data['title'])&&$data['title']){
                $info->title = $data['title'];
            }
            if(isset($data['comment'])&&$data['comment']){
                $info->comment = $data['comment'];
            }
            if(isset($data['status'])){
                $info->status = $data['status'];
            }
            $info->updated_at = time();
            $info->updated_ip = get_client_ip(1);
            if($info->save()){
                return SelfResponse::success('更新成功','b_Role_showRoles');
            }
            return SelfResponse::error('更新失败');

        }else{
            return view('backend::roles.edit_role',[
                'page_title'=>'编辑'.$info->title,
                'info'=>$info
            ]);
        }
    }

    /**
     * @title 角色授权
     * @param Request $request
     * @param int $role_id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function authRole(Request $request,int $role_id)
    {
        $role = RoleModel::find($role_id);
        if($request->isMethod('post')){
            $data = $request->post();
            if($data['my_auth']!= $role->my_auth){
                $role->my_auth = $data['my_auth'];
                $role->updated_at = time();
                $role->updated_ip = get_client_ip(1);
                if($role->save()){
                    return SelfResponse::success('更新成功','b_Role_showRoles');
                }
                return SelfResponse::error('更新失败');
            }
            return SelfResponse::error('角色权限无变化');
        }else{
            return view('backend::roles.auth_role',[
                'page_title'=>'给'.$role->title.'授权',
                'role'=>$role,
                'list'=>json_encode(AuthModel::init()->getDTreeActions())
            ]);
        }
    }

    /**
     * @title 删除角色
     * @param int $role_id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function deleteRole(int $role_id)
    {
        $role = RoleModel::find($role_id);
        if(!$role){
            return SelfResponse::error('角色不存在');
        }
        if($role->delete()){
            return SelfResponse::success('删除成功','b_Role_showRoles');
        }
        return SelfResponse::error('删除失败');
    }


}